package com.artisan.dao;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.artisan.model.Admin;
import com.artisan.model.Student;
import com.artisan.util.StringUtil;

public class StudentDao extends BaseDao {
	public boolean addStudent(Student student) {
		String sql = "insert into s_student values(null,?,?,?,?) ";
		try {
			PreparedStatement preparedStatement = con.prepareStatement(sql);
			preparedStatement.setString(1, student.getName());
			preparedStatement.setInt(2, student.getClassId());
			preparedStatement.setString(3, student.getPassword());
			preparedStatement.setString(4, student.getSex());
			if (preparedStatement.executeUpdate() > 0) {
				return true;
			}
		} catch (SQLException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		}
		return false;
	}

	public List<Student> getStudentList(Student student) {
		List<Student> retList = new ArrayList<Student>();
		StringBuffer sqlString = new StringBuffer("select * from s_student");
		if (!StringUtil.isEmpty(student.getName())) {
			sqlString.append(" and name like '%" + student.getName() + "%'");
		}
		if (student.getClassId() != 0) {
			sqlString.append(" and classId = " + student.getClassId());
		}
		try {
			PreparedStatement preparedStatement = con
					.prepareStatement(sqlString.toString().replaceFirst("and", "where"));
			ResultSet executeQuery = preparedStatement.executeQuery();
			while (executeQuery.next()) {
				Student s = new Student();
				s.setId(executeQuery.getInt("id"));
				s.setName(executeQuery.getString("name"));
				s.setClassId(executeQuery.getInt("classId"));
				s.setSex(executeQuery.getString("sex"));
				s.setPassword(executeQuery.getString("password"));
				retList.add(s);
			}
		} catch (SQLException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		}
		return retList;
	}

	public boolean delete(int id) {
		String sql = "delete from s_student where id = ?";
		try {
			PreparedStatement preparedStatement = con.prepareStatement(sql);
			preparedStatement.setInt(1, id);
			if (preparedStatement.executeUpdate() > 0) {
				return true;
			}
		} catch (SQLException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		}
		return false;
	}

	public boolean update(Student student) {
		String sql = "update s_student set name = ? ,classId = ? , sex = ? ,password = ? where id = ?";
		try {
			PreparedStatement preparedStatement = con.prepareStatement(sql);
			preparedStatement.setString(1, student.getName());
			preparedStatement.setInt(2, student.getClassId());
			preparedStatement.setString(3, student.getSex());
			preparedStatement.setString(4, student.getPassword());
			preparedStatement.setInt(5, student.getId());
			if (preparedStatement.executeUpdate() > 0) {
				return true;
			}
		} catch (SQLException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		}
		return false;
	}

	public String editPassword(Student student, String newPassword) {
		String sql = "select * from s_student where id = ? and password = ?";
		PreparedStatement prst = null;
		int id = 0;
		try {
			prst = con.prepareStatement(sql);
			prst.setInt(1, student.getId());
			prst.setString(2, student.getPassword());
			ResultSet executeQurey = prst.executeQuery();
			if (!executeQurey.next()) {
				String retString = "旧密码错误!";
				return retString;
			}
			id = executeQurey.getInt("id");
		} catch (SQLException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		}

		String retString = "修改失败!";
		String sqlString = "update s_student set password = ? where id = ? ";
		try {
			prst = con.prepareStatement(sqlString);
			prst.setString(1, newPassword);
			prst.setInt(2, id);
			int ret = prst.executeUpdate();
			if (ret > 0) {
				retString = "密码修改成功!";
			}
		} catch (SQLException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		} // 把sql语句传给数据库操作对象
		return retString;
	}

	public Student login(Student student) {
		String sql = "select * from s_student where name = ? and password = ?";
		Student studentRet = null;
		try {
			PreparedStatement prst = con.prepareStatement(sql); // 把sql语句传给数据库操作对象
			prst.setString(1, student.getName());
			prst.setString(2, student.getPassword());
			ResultSet executeQurey = prst.executeQuery();
			if (executeQurey.next()) {
				studentRet = new Student();
				studentRet.setId(executeQurey.getInt("id"));
				studentRet.setClassId(executeQurey.getInt("classId"));
				studentRet.setName(executeQurey.getString("name"));
				studentRet.setPassword(executeQurey.getString("password"));
				studentRet.setSex(executeQurey.getString("sex"));
			}
		} catch (SQLException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		}
		return studentRet;
	}
	
	public boolean enrollStudent(Student student) {
		String sql = "insert into s_student(id,name,password) values(null,?,?) ";
		try {
			PreparedStatement preparedStatement = con.prepareStatement(sql);
			preparedStatement.setString(1, student.getName());
			preparedStatement.setString(2, student.getPassword());
			if (preparedStatement.executeUpdate() > 0) {
				return true;
			}
		} catch (SQLException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		}
		return false;
	}
	
	public boolean isStudentSelected(Student student) {
		String sql = "select * from s_student where name = ? ";
		try {
			PreparedStatement prst = con.prepareStatement(sql);// 把sql语句传给数据库操作对象
			prst.setString(1, student.getName());
			ResultSet executeQuery = prst.executeQuery();
			if (executeQuery.next()) {
				return true;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;
	}
}
